﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Windows.Forms;

namespace GraphsLab5
{
    class Worker
    {
        public static bool reviewData(DataGridView table, PictureBox pict)
        {
            foreach (DataGridViewRow row in table.Rows)
            {
                if (!row.IsNewRow)
                {
                    foreach (DataGridViewColumn col in table.Columns)
                    {
                        if (!reviewCell(table, pict, row.Index, col.Index)) return false;
                    }
                }
            }
            return true;
        }

        public static bool reviewCell(DataGridView table, PictureBox pict, int row, int col)
        {

            string value = "";

            if (table.Rows[row].Cells[col].Value == null && col != 3)
            {
                MessageBox.Show("You have an empty value!");
                return false;
            }

            if (table.Rows[row].Cells[col].Value != null) value = table.Rows[row].Cells[col].Value.ToString();

            switch (col)
            {
                case 0:
                    {
                        int v;
                        try
                        {
                            v = Convert.ToInt32(value);
                        }
                        catch
                        {
                            MessageBox.Show("Wrong Value in Points (isn't a number)!");
                            table.Rows[row].Cells[col].Value = "";
                            return false;
                        }
                        if (v < 0 || ((v > pict.Size.Width && col == 0) 
                            || (v > pict.Size.Height && col == 1)))
                        {
                            MessageBox.Show("Wrong Value in Points (not in bounds)!");
                            table.Rows[row].Cells[col].Value = "";
                            return false;
                        }
                        break;
                    }
                case 1:
                    {
                        goto case 0;
                    }
                case 2:
                    {
                        if (value != "Black" && value != "Red" && value != "Green" && value != "Blue")
                        {
                            MessageBox.Show("Wrong Value in Colors!");
                            table.Rows[row].Cells[col].Value = "";
                            return false;
                        }
                        break;
                    }
                case 3:
                    {
                        bool flag = false;
                        while (value.Length > 0)
                        {
                            if (!Char.IsDigit(value[0]) && value[0] != ',')
                            {
                                MessageBox.Show("Wrong Value in Lines!");
                                table.Rows[row].Cells[col].Value = "";
                                return false;
                            }
                            if (!flag)
                            {
                                int i = 0;
                                while (i < value.Length && Char.IsDigit(value[i]))
                                {
                                    i++;
                                    flag = true;
                                }
                                value = value.Remove(0, i);
                            }
                            else
                            {
                                if (value[0] == ',')
                                {
                                    value = value.Remove(0, 1);
                                    flag = false;
                                }
                            }
                        }
                        break;
                    }
            }
            return true;
        }

        public static string parseToChange(string src, int n)
        {
            if (src.Length == 0) return src;
            string[] str = src.Split(',');
            List<int> list = new List<int>();
            foreach (string s in str)
            {
                if (s != "")
                {
                    int num = Int32.Parse(s);
                    if (num < n) list.Add(num);
                    if (num > n) list.Add(num - 1);
                }
            }
            string res = "";
            foreach (int num in list)
            {
                res += num.ToString() + ",";
            }
            if (res.Length > 1) res = res.Remove(res.Length - 1);
            return res;
        }

        public static Matrix createMatrix(List<List<int>> lines)
        {
            if (lines.Count > 0)
            {
                Matrix a = new Matrix(lines.Count);
                for (int i = 0; i < lines.Count; i++)
                {
                    if (lines[i].Count > 0)
                    {
                        foreach (int k in lines[i])
                        {
                            a[i, k - 1] = true;
                            a[k - 1, i] = true;
                        }
                    }
                }
                return a;
            }
            return new Matrix(0);
        }

    }
}
